package com.hongyuan.venue.ui.home.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.venue.R;
import com.hongyuan.venue.base.AppViewModelFactory;
import com.hongyuan.venue.base.CustomActivity;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.databinding.ActivityPreordainBinding;
import com.hongyuan.venue.ui.home.adapter.CustomAdapter;
import com.hongyuan.venue.ui.home.beans.SecheduleBeans;
import com.hongyuan.venue.ui.home.view_mode.ScheduledViewModel;
import com.hongyuan.venue.utils.TimeUtil;

import me.tatarka.bindingcollectionadapter2.BR;

public class ScheduledActivity extends CustomActivity<ActivityPreordainBinding, ScheduledViewModel> {

    private CustomAdapter adapter;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_preordain;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public ScheduledViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(ScheduledViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        binding.comBack.setOnClickListener(view -> finish());

        adapter = new CustomAdapter(this, blockListener);
        binding.excelPanel.setAdapter(adapter);
    }

    private View.OnClickListener blockListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SecheduleBeans.ListBean cell = (SecheduleBeans.ListBean) view.getTag();
            if (cell != null) {
                if(cell.getIs_reserve() == 1){
                    TextView textView = (TextView)view;

                    if(cell.isSelect()){
                        textView.setTextColor(ContextCompat.getColor(ScheduledActivity.this, R.color.theme_333333));
                        view.setBackgroundColor(ContextCompat.getColor(ScheduledActivity.this, R.color.theme_FFFFFF));
                        cell.setSelect(false);
                        //改变选取费用
                        viewModel.changeAllPrice("-"+cell.getType_price());
                        viewModel.selectAllBean.get().remove(cell);
                    }else{
                        textView.setTextColor(ContextCompat.getColor(ScheduledActivity.this, R.color.theme_FFFFFF));
                        view.setBackgroundColor(ContextCompat.getColor(ScheduledActivity.this, R.color.theme_23C095));
                        cell.setSelect(true);

                        cell.setStart_time(TimeUtil.getStringByFormat(cell.getS_time()*1000,TimeUtil.dateFormatYMDHMS));
                        cell.setEnd_time(TimeUtil.getStringByFormat(cell.getE_time()*1000,TimeUtil.dateFormatYMDHMS));
                        cell.setPrice(cell.getType_price());

                        //改变选取费用
                        viewModel.changeAllPrice(cell.getType_price());
                        viewModel.selectAllBean.get().add(cell);
                    }

                }
            }
        }
    };

    @Override
    public void initData() {
        super.initData();

        //获取时间轴
        viewModel.clearParams().setParams("gc_id",getBundle().getString("gc_id"));
        viewModel.requestData(Constants.GET_PLAN_DATE);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showContents.observe(this, secheduleBeans -> {
            //清除下数据
            viewModel.allPrice.set("0");

            if(secheduleBeans.getName() != null && secheduleBeans.getName().size() > 0){
                binding.isEmpty.setVisibility(View.GONE);
                binding.excelPanel.setVisibility(View.VISIBLE);

                //设置高度比例
                adapter.setMultiple(secheduleBeans.getHeightMap());
                //设置行列数
                adapter.setMaxNums(secheduleBeans.getMaxNums());
                //设置数据
                adapter.setAllData(secheduleBeans.getArr_time(), secheduleBeans.getName(), secheduleBeans.getList());
            }else{
                binding.isEmpty.setVisibility(View.VISIBLE);
                binding.excelPanel.setVisibility(View.GONE);
            }


        });
    }
}
